package logic

import (
	"context"
	"encoding/json"
	"github.com/olivere/elastic/v7"
	"server/global"
	"server/models"
	"server/proto"
)

func (s *ServerRpc) EsHigh(ctx context.Context, in *proto.EsHighReq) (*proto.EsHighRes, error) {
	//todo: es
	high := elastic.NewHighlight()
	text := elastic.NewHighlighterField("name")
	text.PreTags("<span color='red'>").PostTags("</span>")
	high.Fields(text)
	quire := elastic.NewMultiMatchQuery(in.Word, "goods_name", "goods_address")
	data, err := models.EsHigh(in.Index, high, quire, int(in.Page), int(in.Size))
	res, _ := json.Marshal(data)
	if err != nil {
		return &proto.EsHighRes{}, err
	}
	key := "EsLI:" + in.Userid
	//todo: redis  存一份历史记录  有序列表，每查一次+1
	global.Redis.ZIncrBy(context.Background(), key, 1, in.Word)
	return &proto.EsHighRes{
		Data: res,
	}, nil
}
